package com.zhu.oj.judge.codeSandBox.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.zhu.oj.common.enums.ErrorCode;
import com.zhu.oj.common.exception.BusinessException;
import com.zhu.oj.judge.codeSandBox.CodeSandBox;
import com.zhu.oj.judge.codeSandBox.model.ExecuteCodeRequest;
import com.zhu.oj.judge.codeSandBox.model.ExecuteCodeResponse;
import org.apache.commons.lang3.StringUtils;

/*
实际调用远程沙箱的接口
 */
public class RemoteCodeSandBox implements CodeSandBox {
    public static final String AUTH_REUQUEST_HEADER = "auth";
    private static final String AUTH_REQUEST_SECRET = "secretKey";
    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {

        String url = "http://localhost:8090/executeCode";
        String s = JSONUtil.toJsonStr(executeCodeRequest);
        String body = HttpUtil.createPost(url).header(AUTH_REUQUEST_HEADER,AUTH_REQUEST_SECRET).body(s).execute().body();//获得响应体内容
        if(StringUtils.isBlank(body)){
            throw new BusinessException(ErrorCode.API_REQUEST_ERROR , "执行远程沙箱错误：" + body);
        }
        return JSONUtil.toBean(body,ExecuteCodeResponse.class);
    }
}
